package com.uniview.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uniview.dao.MenusDao;
import com.uniview.dao.ResourceRoleDao;
import com.uniview.entity.po.Menu;
import com.uniview.entity.po.ResourceRole;
import com.uniview.entity.vo.MenuRole;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.QueryBuilder;
import com.uniview.service.MenuService;

@Service("menuService")
public class MenuServiceImpl implements MenuService {
	
	Logger logger = Logger.getLogger(MenuServiceImpl.class);
	
	@Autowired
	protected MenusDao menuDao;
	
	@Autowired
	protected ResourceRoleDao resourceRoleDao;

	@Override
	public int addMenu(Menu menu) {
		try{
			return menuDao.addMenu(menu);
		}catch(Exception e){
			logger.error("新增菜单异常");
		}
		return 0;
	}

	@Override
	public boolean updateMenu(Menu menu) {
		try{
			return menuDao.update(menu);
		}catch(Exception e){
			logger.error("修改菜单异常");
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean deleteMenu(int mId) {
		try{
			return menuDao.deleteMenu(mId);
		}catch(Exception e){
			logger.error("删除菜单异常");
		}
		return false;
	}

	@Override
	public List<Menu> findAllMenu() {
		try{
			return menuDao.findAllMenu();
		}catch(Exception e){
			logger.error("查询菜单异常");
		}
		return null;
	}

	@Override
	public List<Menu> findAllClid() throws Exception {
		try{
			return menuDao.findAllClid();
		}catch(Exception e){
			logger.error("查询菜单异常");
		}
		return null;
	}

	@Override
	public List<Menu> findMenuByParentId(int parentId) {
		try{
			return menuDao.findMenuByParentId(parentId);
		}catch(Exception e){
			logger.error("查询菜单异常");
		}
		return null;
	}

	@Override
	public List<MenuRole> findMenuWithRoleId(int roleId) {
		try{
			List<Menu> menuList = this.findAllMenu();
			if(!menuList.isEmpty()){
				List<ResourceRole> rrList = resourceRoleDao.findResouceRoleWithRoleId(roleId);
				return checkMenuRole(menuList,rrList);
			}
		}catch(Exception e){
			logger.error("查询角色授权的资源列表异常");
		}
		return null;
	}
	
	
	/**
	 * 检查资源对应的权限信息
	 * @param menuList
	 * @param rrList
	 * @return
	 */
	private List<MenuRole> checkMenuRole(List<Menu> menuList,List<ResourceRole> rrList){
		List<MenuRole> mrList = new ArrayList<MenuRole>();
		for(Menu menu : menuList){
			boolean isCheck = false;
			if(!rrList.isEmpty()){
				for (ResourceRole resourceRole : rrList) {
					if(menu.getMenuId() == resourceRole.getMenuId()){
						isCheck = true;
						break;
					}
				}
			}
			MenuRole menuRole = new MenuRole();
			menuRole.setMenuId(menu.getMenuId());
			menuRole.setMenuName(menu.getMenuName());
			menuRole.setChecked(isCheck);
			
			List<Menu> children = this.findMenuByParentId(menu.getMenuId());
			if(!children.isEmpty()){
				List<MenuRole> cMrList = this.checkMenuRole(children, rrList);
				menuRole.setChildrenMenuRole(cMrList);
			}
			
			mrList.add(menuRole);
		}
		return mrList;
	}

	@Override
	public Menu findMenuById(int menuId) {
		try {
			return menuDao.findMenuById(menuId);
		} catch (Exception e) {
			logger.error("根据ID查询菜单信息异常", e);
		}
		return null;
	}

	@Override
	public List<Menu> findParentMenu(Integer menuType) {
		try {
			return menuDao.findParentMenu(menuType);
		} catch (Exception e) {
			logger.error("查询父节点菜单异常", e);
		}
		return null;
	}

	@Override
	public Menu findMenuByMenuUrl(String url) {
		try {
			return menuDao.findMenuByMenuUrl(url);
		} catch (Exception e) {
			logger.error("查询菜单异常", e);
		}
		return null;
	}

	@Override
	public List<Menu> findSubMenusByRoleId(Integer roleId) {
		try {
			return menuDao.findSubMenusByRoleId(roleId);
		} catch (Exception e) {
			logger.error("查询菜单异常", e);
		}
		return new ArrayList<Menu>();
	}
	
	public Pager<Menu> findMenuByPager(QueryBuilder queryBuilder, int pageSize, int pageNo) {
		try {
			return menuDao.findMenuByPage(queryBuilder, pageNo, pageSize);
		} catch (Exception e) {
			logger.error("分页查询菜单列表异常", e);
		}
		return null;
	}

	public Integer findMenuCountByCondition(String menuName, Integer menuId) throws Exception{
		return menuDao.findMenuCountByCondition(menuName, menuId);
	}

	@Override
	public List<Menu> findMenuByEmployeeId(String empCode) throws Exception {
		return menuDao.findMenuByEmployeeId(empCode);
	}
	
}
